#include <linux/kernel.h>
#include <linux/netdevice.h>
#include <linux/module.h>
#include <linux/vmalloc.h>
#include "utils/utils.h"

/** module bitmap for dbg print */
uint ar_dbg_mod = AR_DEBUG_ALL;
/** Debug level for dbg print */
uint ar_dbg_lv = DEFAULT_DEBUG_LVL;
/** Misc control such as pm */
u8 ar_misc_ctrl = 0;

/* control the debug chardev module */
u8 ar_dbg_mod_chardev = 0;
/* control the debug chardev level */
u8 ar_dbg_lv_chardev = 0;
/* Whether enable the debug chardev*/
int ar_dbg_chardev_enable = 0;

/**
 *  @brief      Dump the data in hex
 *
 *  @param      lvl  Debug level to print, refers to DL_xxx
 *  @param      note Dump message
 *  @param      data Data to print
 *  @param      len  Length of the data
 *
 *  @return     N/A
 */
void dump_bytes(uint lvl, char *note, u8 *data, int len)
{
    char hex2ch[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
    int rest = len;
    u8 *buf = data;
    char tmp[100]; //16*3+1+16+1
    int byte_to_print, i, j;

    if (lvl > ar_dbg_lv) {
        return;
    }

    AR_DBG(DM_GENERIC, lvl, "%s : dump addr=%p len=%d\n", note, data, len);

    while (rest > 0)
    {
        byte_to_print = (rest>=16)?16:rest;

        for(i=0; i<16; i++)
        {
            if (i<byte_to_print)
            {
                tmp[i*3] = hex2ch[buf[i] >> 4];
                tmp[i*3+1] = hex2ch[buf[i] & 0xf];
            }
            else
            {
                tmp[i*3] = ' ';
                tmp[i*3+1] = ' ';
            }
            tmp[i*3+2] = (i==7)?'-':' ';
        }
        tmp[i*3] = '|';
        for(j=0; j<byte_to_print; j++)
        {
            if ((buf[j] >= ' ') && (buf[j] <= '~'))
                tmp[i*3+1+j] = buf[j];
            else
                tmp[i*3+1+j] = '.';
        }
        tmp[i*3+1+j] = 0;
        AR_DBG(DM_GENERIC, lvl, "%s\n", tmp);

        buf += byte_to_print;
        rest -= byte_to_print;
    }
}


module_param(ar_dbg_lv, uint, 0);
MODULE_PARM_DESC(ar_dbg_lv, "Debug level");

module_param(ar_dbg_mod, uint, 0);
MODULE_PARM_DESC(ar_dbg_mod, "Debug module");
